import REQ, {ResData} from "@/utils/ApiUtil.ts";
import {IPage, PojInfoDto, PojInfoVo} from "@/api/Types/ObjInterfaces.ts";

class PojApi {
    public getPojTypes = async (): Promise<string[]> => {
        const res = await REQ<null, ResData<string[]>>({
            method: 'get',
            url: '/api/poj/type'
        });
        return res.data
    }

    public savePojInfo = async (data?: PojInfoVo) => {
        return await REQ<null, ResData<void>>({
            method: 'post',
            url: 'api/poj',
            data: data,
        });
    }

    public pagePoj = async (current?: number, size?: number, type?: string): Promise<IPage<PojInfoDto>> => {
        if (current == null) {
            current = 1
        }
        if (size == null) {
            size = 10
        }

        const res = await REQ<null, ResData<IPage<PojInfoDto>>>({
            method: 'get',
            url: 'api/poj/page',
            params: {
                current: current,
                size: size,
                type: type
            }
        })
        return res.data
    }

    public deletePoj = async (value: string) => {
        return await REQ<string, ResData<void>>({
            method: 'get',
            url: 'api/poj/delete/' + value
        })
    }

    public addObjToPoj = async (objIdList: string[], pojId: string) => {
        return await REQ({
            method: 'post',
            url: '/api/poj/obj/' + pojId,
            data: objIdList
        })
    }

    public removeObjFromPoj = async (objIdList: string[], pojId: string) => {
        return await REQ({
            method: 'post',
            url: '/api/poj/obj/remove/' + pojId,
            data: objIdList
        })
    }

    public getPojById = async (pojId: string) => {
        const data = await REQ<null, ResData<PojInfoDto>>({
            method: 'get',
            url: '/api/poj/' + pojId
        })
        return data.data
    }
}

export default new PojApi()